<?php

namespace App\Http\Model;

use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Redis;

class RoomVoiceCheck extends Base
{
    protected $mapRoomVoiceCheckTask = 'ROOM:VOICE:CHECK:TASK';
    protected $mapRoomVoiceCheckConfig = 'ROOM:VOICE:CHECK:CONFIG';
    protected $mapRoomVoiceCheckHandleUserPrefix = 'ROOM:VOICE:CHECK:HANDLE:';
    protected $mapRoomVoiceCheckHigh = 'ROOM:VOICE:CHECK:HIGH'; //高频检测
    protected $mapRoomVoiceCheckHighShunt = 'ROOM:VOICE:CHECK:HIGHSHUNT'; //高频分流检测
    protected $mapRoomVoiceCheckWaitTime = 'ROOM:VOICE:CHECK:WAITING:TIME:';//音频监测等待期(每5分钟 检测3分钟)

    public function __construct(array $attributes = [])
    {
        parent::__construct($this->table, $attributes);
    }

    /**
     * 添加检测任务
     * @param $roomId
     * @param $taskId
     * @param $time
     * @return mixed
     */
    public function addTask($roomId, $taskId, $interval, $checkTime)
    {
        Redis::SETEX($this->mapRoomVoiceCheckWaitTime.$roomId ,$interval-5 ,$taskId); //stop 任务，5分钟内不添加（避免临界点5分钟
        return Redis::HSET($this->mapRoomVoiceCheckTask, $roomId, $taskId . ',' . $checkTime);
    }

    /**获取音频监测等待时间
     * @param $roomId
     * @return mixed
     */
    public function getTaskWaitTime($roomId){
        return Redis::GET($this->mapRoomVoiceCheckWaitTime.$roomId);
    }
    /**
     * 获取检测检测任务
     * @param $roomId
     * @return array|null
     */
    public function getTask($roomId)
    {
        $data = Redis::HGET($this->mapRoomVoiceCheckTask, $roomId);
        if (empty($data)){
            return null;
        }
        return explode(',', $data);
    }

    /**
     * 获取全部检测任务
     * @return array
     */
    public function getTaskAll()
    {
        $data = Redis::HGETALL($this->mapRoomVoiceCheckTask);
        if (empty($data)){
            return [];
        }
        foreach ($data as $k => $v){
            $data[$k] =  explode(',', $v);
        }
        return $data;
    }

    /**
     * 删除房间检测任务
     * @param $roomId
     * @return mixed
     */
    public function delTask($roomId)
    {
        return Redis::HDEL($this->mapRoomVoiceCheckTask, $roomId);
    }


    /**
     * 设置间隔时间
     * @param $intervalTime
     * @return mixed
     */
    public function setConfigIntervalTime($intervalTime)
    {
        return Redis::HSET($this->mapRoomVoiceCheckConfig, 'INTERVALTIME', $intervalTime);
    }


    /**
     * 获取间隔时间
     * @return float|int
     */
    public function getConfigIntervalTime()
    {
        $ret = Redis::HGET($this->mapRoomVoiceCheckConfig, 'INTERVALTIME');
        if (empty($ret)){
            return 0;
        }
        return intval($ret);
    }


    /**
     * 设置检测连续时长
     * @param $duration
     * @return mixed
     */
    public function setConfigDuration($duration)
    {
        return Redis::HSET($this->mapRoomVoiceCheckConfig, 'DURATION', $duration);
    }


    /**
     * 获取检测连续时长
     * @return float|int
     */
    public function getConfigDuration()
    {
        $ret = Redis::HGET($this->mapRoomVoiceCheckConfig, 'DURATION');
        if (empty($ret)){
            return 0;
        }
        return intval($ret);
    }

    /**
     *  设置检查项
     * @param $items array
     * @return mixed int
     */
    public function setConfigCheckItems($items) {
        return Redis::HSET($this->mapRoomVoiceCheckConfig, 'CHECKITEMS', json_encode($items));
    }

    /**
     * 获取检查项
     * @return array
     */
    public function getConfigCheckItems() {
        $res = Redis::HGET($this->mapRoomVoiceCheckConfig, 'CHECKITEMS');
        if(!empty($res)) {
            return json_decode($res, true);
        }
        return [];
    }

    /**
     * 设置最后检测时间
     * @param $duration
     * @return mixed
     */
    public function setConfigLast($time)
    {
        return Redis::HSET($this->mapRoomVoiceCheckConfig, 'LASTCHACK', $time);
    }


    /**
     * 获取最后检测时间
     * @return float|int
     */
    public function getConfigLast()
    {
        $ret = Redis::HGET($this->mapRoomVoiceCheckConfig, 'LASTCHACK');
        if (empty($ret)){
            return 0;
        }
        return intval($ret);
    }

    /**
     * 设置关闭状态
     * @param $state int 0 停用 1 间断检测 2 不间断
     * @return mixed
     */
    public function setState($state) {
        return Redis::HSET($this->mapRoomVoiceCheckConfig,'STATE', $state);
    }

    /**
     * @return mixed
     */
    public function getState() {
        $ret =  Redis::HGET($this->mapRoomVoiceCheckConfig,'STATE');
        if (empty($ret)){
            return 0;
        }
        return intval($ret);
    }

    /**
     * @param $user_id
     * @param $type
     * @return mixed
     */
    public function setHandleUser($user_id, $type) {
        return Redis::SET($this->mapRoomVoiceCheckHandleUserPrefix. "{$user_id}:{$type}", time(), 'EX', 60*60);
    }

    /**
     * 获取需要高频分流检测的房间列表
     */
    public function getLoopShuntRoomIdArr(){
        $roomIdArr = Redis::HKEYS($this->mapRoomVoiceCheckHighShunt);
        return $roomIdArr;
    }

    /**
     * @param $user_id
     * @param $type
     * @param $handle_method
     * @return mixed
     */
    public function checkIsHandle($user_id, $type) {
        return Redis::EXISTS($this->mapRoomVoiceCheckHandleUserPrefix. "{$user_id}:{$type}");
    }


    /**
     * 查询房间是高频检测
     * @param $roomId
     * @return mixed
     */
    public function isHighRoom($roomId){
        return Redis::HEXISTS($this->mapRoomVoiceCheckHigh, $roomId);
    }

    /**
     * 查询房间是否为高频分流检测
     * @param $roomId
     * @return mixed
     */
    public function isHighShuntRoom($roomId){
        return Redis::HEXISTS($this->mapRoomVoiceCheckHighShunt, $roomId);
    }


    /**
     * 获取所有高频检测房间
     * @return mixed
     */
    public function getHighRooms(){
        return Redis::HKEYS($this->mapRoomVoiceCheckHigh);
    }


    /**
     * 添加高频检测房间
     * @return mixed
     */
    public function addHighRoom($roomId){
        return Redis::HSET($this->mapRoomVoiceCheckHigh, $roomId, time());
    }

    /**
     * 删除高频检测房间
     * @return mixed
     */
    public function delHighRoom($roomId){
        return Redis::HDEL($this->mapRoomVoiceCheckHigh, $roomId);
    }

}
